#! /usr/bin/env python
#coding=utf-8
'''
作者：gdlmo
邮箱：gytlgac@163.com
日期: 2017-01-16
版本：1.0
'''
import urllib.request
import re
from pyquery import PyQuery as pq
from lxml import etree
class QSBK:
    #定义初始化方法，定义一些全局使用的变量
    def __init__(self):
        self.pageIndex = 1
        #self.userAgent = 'Mozilla/5.0 (Windows NT 10.0; WOW64)'
        #self.headers = {'User-Agent', self.userAgent}
        self.headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64)'}
        self.enabled = False
        self.url = 'http://www.qiushibaike.com/hot/page/'
        self.sotries = []
        print('初始化对象')
    #读取网页，并返回网页源码
    def getPage(self, pageIndex):
        try:
            #构建请求的request
            request = urllib.request.Request(self.url + str(pageIndex), headers=self.headers)
            response = urllib.request.urlopen(request)
            pageContent = response.read().decode('utf-8')
            return pageContent
        except urllib.request.URLError as e:
            if hasattr(e, 'code'):
                print(e.code)
            if hasattr(e, 'reason'):
                print(e.reason)

    #使用pyquery解析，并返回解析后的pyquery对角组
    def getSubItems(self, content, selector):
        doc = pq(content)
        lists = doc(selector)
        return lists

    #解析并返回
    def getPageItems(self, pageIndex):
        content = self.getPage(pageIndex)
        storyCodelists = self.getSubItems(content, 'body div.article')
        pageStories = []
        for storycode in storyCodelists.items():
            story = self.getSubItems(storycode.html(), 'div')
            pageStories.append(story.text())
        return pageStories;

    #
    def loadPage(self):
        if self.enabled == True:
            if len(self.sotries) < 2:
                pageStories = self.getPageItems(self.pageIndex)
                if pageStories:
                    self.sotries.append(pageStories)
                return;

    def getOneStory(self, pageStories):
        for story in pageStories:
            userinput = input()
            if userinput == 'Q':
                self.enabled = False
                return
            print(story)
            self.loadPage()

    def start(self):
        print('正在读取糗事百科,按回车查看新段子，Q退出')
        self.enabled = True
        self.loadPage()
        while self.enabled:
            if len(self.sotries)>0:
                pageStories = self.sotries[0]
                del self.sotries[0]
                self.getOneStory(pageStories)
spider = QSBK()
spider.start()
